﻿using EMS.system.Entityies.MasterData.MaterialManagementModel;
using EMS.system.Entityies.MasterData.MaterialTypeModel;
using EMS.system.IEntityAppService.MasterData.MaterialManagementService;
using EMS.system.IEntityAppService.MasterData.MaterialManagementService.MaterialManagementDTO;
using EMS.system.IEntityAppService.MasterData.MaterialTypeIService.MaterialTypeDTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace EMS.system.EntityAppService.MasterData.MaterialManagementService
{
    public class MaterialManagementAppService:ApplicationService, IMaterialManagementAppService
    {
        IRepository<MaterialManagement> _repository;
        IRepository<MaterialType> _repositoryType;

        public MaterialManagementAppService(IRepository<MaterialManagement> repository, IRepository<MaterialType> repositoryType)
        {
            _repository = repository;
            _repositoryType = repositoryType;
        }

        /// <summary>
        /// 调用递归获取物料类型数据
        /// </summary>
        /// <param name="materialTypeName"></param>
        /// <returns></returns>
        public async Task<List<MaterialTypeTreeDto>> GetMaterialType()
        {
            var materialTypeList = await _repositoryType.GetListAsync();
            var materialTypes = await GetMaterialType(materialTypeList, 0);
            return materialTypes;
        }

        /// <summary>
        /// 递归物料类型
        /// </summary>
        /// <param name="materialTypeList"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<MaterialTypeTreeDto>> GetMaterialType(List<MaterialType> materialTypeList, int id = 0)
        {
            var list = materialTypeList.Where(x => x.ParentId == id);
            List<MaterialTypeTreeDto> treedtoList = new List<MaterialTypeTreeDto>();
            foreach (var item in list)
            {
                MaterialTypeTreeDto selectDTO = new MaterialTypeTreeDto()
                {
                    Id = item.Id,
                    Title = item.MaterialTypeName,
                };
                selectDTO.Children = GetMaterialType(materialTypeList, item.Id).Result;
                treedtoList.Add(selectDTO);
            }
            return treedtoList;
        }

        /// <summary>
        /// 物料新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> MaterialAdd(MaterialManagementAddDto model)
        {
            var materialModel = ObjectMapper.Map<MaterialManagementAddDto, MaterialManagement>(model);
            await _repository.InsertAsync(materialModel);
            return 1;
        }


        /// <summary>
        /// 获取所有物料数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<MaterialManagement>> GetMaterialAll()
        {
            var list = await _repository.GetListAsync();
            return list;
        }

        /// <summary>
        /// 物料删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> MaterialDelete(int id)
        {
            await _repository.DeleteAsync(x=>x.Id == id);
            return 1;
        }

        /// <summary>
        /// 获取单个物料
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<MaterialManagement> GetSingMaterial(int id)
        {
            return await _repository.FindAsync(x => x.Id == id);
        }

        /// <summary>
        /// 物料编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> MaterialUpdate(MaterialManagementUpdDto model)
        {
            var materialModel = ObjectMapper.Map<MaterialManagementUpdDto, MaterialManagement>(model);
            await _repository.UpdateAsync(materialModel);
            return 1;
        }
    }
}
